package com.example.finalexam;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class ReciteActivity extends AppCompatActivity{
    private static final String TAG ="ReciteActivity";
    TextView word,translate;
    Button button_renshi,button_burenshi,button_next,button_restart;
    ArrayList<Word> words;
    DBHelper dbHelper;
    int i=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbHelper=new DBHelper(this);
        setContentView(R.layout.activity_recite);
        word = findViewById(R.id.recite_word);
        translate = findViewById(R.id.recite_translate);
        button_renshi = findViewById(R.id.button_renshi);
        button_burenshi = findViewById(R.id.button_burenshi);
        button_next = findViewById(R.id.button_next);
        button_restart = findViewById(R.id.button_restart);
        DBManager dbManager=new DBManager(this);
        words=dbManager.getWords();
        //若单词库中没有数据
        String wordyan = null;
        try {
            wordyan = words.get(i).en_meaning;
        }
        catch (Exception e){
              Toast.makeText(this, "单词库中没有数据", Toast.LENGTH_SHORT).show();
        }
        word.setText(words.get(i).en_meaning);
        translate.setText("");
    }
    //点击认识单词则将该单词从数组中删除，在这一轮中不在出现
    public void renshi(View view){
        if (words.size() == 0)
                {
                    Toast.makeText(ReciteActivity.this,"没有单词啦！",Toast.LENGTH_SHORT).show();
                } else{
                    words.remove(i);  //删除该单词
                    i = (int)(Math.random()*words.size());
                    try {
                        word.setText(words.get(i).en_meaning);
                    } catch (Exception e) {
                        e.printStackTrace();
                        word.setText("");
                    }
                    translate.setText("");
    }
    }
    //点击不认识，显示该单词的翻译
    public void burenshi(View view){
        if(words.size() == 0){
                    Toast.makeText(ReciteActivity.this,"没有单词啦！",Toast.LENGTH_SHORT).show();
                }else{
                    translate.setText(words.get(i).cn_meaning);
                }
    }
    //点击下一个，跳到下一个单词
    public void next(View view){
        i = (int)(Math.random()*words.size());
                try {
                    word.setText(words.get(i).en_meaning);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                translate.setText("");
    }
    //退出背单词，返回主页面
    public void fClick(View view){
        Intent intent=new Intent(ReciteActivity.this,MainActivity.class);
        startActivity(intent);
    }


}